// index.js
// 获取应用实例
import {
    request,
} from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
import {
    getSetting,
    openSetting,
    showModal,
    showToast,
    getUserProfile,
    checkImgExists
} from "../../utils/asyncWx.js";


// pages/home/home.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hasReserved:false,
        position:{},

    },

    tryLogin(){
        wx.login({
            success: (res) => {
                let code = res.code; 
                request({
                    url: "user/wxlogin",
                    method: "POST",
                    header: { 'content-type': 'application/x-www-form-urlencoded' },
                    data: { code: code }
                }).then((result, err) => {
                    if (result) {
                        wx.setStorageSync('token', result)
                        this.checkReservation();
                    } else {
                            this.tryLogin()                      
                    }
                })
            }
        })
      
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
    },

    onShow(){
        this.tryLogin();
    },
    async checkReservation(){
        const position=await request({
            url:'queue/query',
        })
        if(position.queueID==null){
            this.setData({hasReserved:false});
        }else{
            this.setData({
                hasReserved:true,
                position
            })
        }
    },

    handleRefresh(){
        this.checkReservation()
    },

    handleCancel(){
        request({
            url:"queue/exit",
            data:{
                queueID:this.data.position.queueID,
                state:"1"
            },
            method:"POST",
            header:{ 'content-type': 'application/x-www-form-urlencoded' }
        })
        this.setData({
            hasReserved:false
        })
       
    }
})